package com.rattsoftware.client.controllers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.RattsAsyncCallback;
import com.rattsoftware.client.RefreshableEvent;
import com.rattsoftware.client.RefreshablePublisher;
import com.rattsoftware.client.webclient;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.model.UserSummary;
import com.rattsoftware.client.service.UserServiceAsync;
import com.rattsoftware.client.util.PagingUtil;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.client.views.UserListView;
import com.rattsoftware.server.admin.model.User;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;

public class UserListController extends Controller {
    AppMessages appMessages = GWT.create(AppMessages.class); 

    private UserListView userListView;
    private UserServiceAsync userService;
    
    public UserListController() {
        super();
        registerEventTypes(AppEvents.UserList);
    }
    
    @Override
    protected void initialize() {
        GWT.log("UserListController : initialize");
        userService = Registry.get(webclient.USER_SERVICE);
        userListView = new UserListView(this);
        RefreshablePublisher.get().subscribe(RefreshableEvent.Type.UPDATE_USER, userListView);
        RefreshablePublisher.get().subscribe(RefreshableEvent.Type.CREATE_USER, userListView);
        RefreshablePublisher.get().subscribe(RefreshableEvent.Type.REFRESH_USERLIST, userListView);
    }

    @Override
    public void handleEvent(AppEvent event) {
    	GWT.log("UserListController : handleEvent");
        EventType type = event.getType();
        if (type == AppEvents.UserList) {
            forwardToView(userListView, event);
        }
    }
    
    public void getUsers(final PagingLoadConfig pagingLoadConfig, 
            final AsyncCallback<com.extjs.gxt.ui.client.data.PagingLoadResult<UserSummary>> callback) {
    	GWT.log("UserListController : getting Users");
    	/*userService.getUsers(PagingUtil.createPagingLoadConfig(pagingLoadConfig), 
    			new RattsAsyncCallback<PagingLoadResult<User>>() {
            @Override
			public void onSuccess(PagingLoadResult<User> result) {*/
    	
                List<UserSummary> results = new ArrayList<UserSummary>();
                //List<User> data = result.getData();
                List<User> data = TestData.getUsers();
                for (User d : data) {
                    results.add(new UserSummary(d));
                }
                ProgressIndicator.hideProgressBar();
                //callback.onSuccess(new BasePagingLoadResult<UserSummary>(results, result.getOffset(), result.getTotalLength()));
                callback.onSuccess(new BasePagingLoadResult<UserSummary>(results, 0, 20));
            /*}
    	});*/
    }
    
    public void forwardToNewWizard() {
    	GWT.log("UserListController : forwardToNewWizard");
        Dispatcher dispatcher = Dispatcher.get();
        AppEvent event = new AppEvent(NewEditUserController.NEWUSER);
        GWT.log("dispatching event=" +event);
    	dispatcher.dispatch(event);
    }
    
    public void forwardToEditWizard(UserSummary userSummary){
       	GWT.log("UserListController : forwardToEditWizard");
        Dispatcher dispatcher = Dispatcher.get();
        AppEvent event = new AppEvent(NewEditUserController.EDITUSER);
        event.setData("user", userSummary.getUser());
    	dispatcher.dispatch(event);
    }
}